package edu.friday.controller;

import edu.friday.common.base.BaseController;
import edu.friday.common.constant.UserConstants;
import edu.friday.common.result.RestResult;
import edu.friday.common.result.TreeSelect;
import edu.friday.common.security.LoginUser;
import edu.friday.common.security.service.TokenService;
import edu.friday.model.SysMenu;
import edu.friday.model.vo.SysMenuVO;
import edu.friday.service.ISysMenuService;
import edu.friday.utils.BeanUtils;
import edu.friday.utils.StringUtils;
import edu.friday.utils.http.ServletUtils;
import edu.friday.utils.security.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("/system/menu")
@Validated
public class SysMenuController extends BaseController {

    @Autowired
    private ISysMenuService iSysMenuService;

    @Autowired
    private TokenService tokenService;

    @GetMapping("/list")
    public RestResult list(SysMenuVO sysMenuVO) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        List<SysMenu> menus = iSysMenuService.selectMenuList(sysMenuVO, userId);
        return RestResult.success(menus);
    }

    @GetMapping("/treeSelect")
    public RestResult treeSelect(SysMenuVO sysMenuVO) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        List<SysMenu> menus = iSysMenuService.selectMenuList(sysMenuVO, userId);
        List<SysMenuVO> sysMenuVOS = BeanUtils.copyProperties(menus, SysMenuVO.class);
        List<TreeSelect> treeList = iSysMenuService.buildMenuTreeSelect(sysMenuVOS);
        return RestResult.success(treeList);
    }

    @GetMapping("/roleMenuTreeSelect/{roleId}")
    public RestResult RoleMenuTreeSelect(@PathVariable Long roleId) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        //SecurityUtils.getAuthentication();
        RestResult success = RestResult.success();
        //获取菜单树列表
        List<SysMenu> menus = iSysMenuService.selectMenuList(userId);
        List<SysMenuVO> sysMenuVOS = BeanUtils.copyProperties(menus, SysMenuVO.class);
        List<TreeSelect> treeList = iSysMenuService.buildMenuTreeSelect(sysMenuVOS);
        success.put("menus",treeList);
        List<Long> roleMenuTreeList = iSysMenuService.selectMenuListByRoleId(roleId);
        success.put("checkedKeys",roleMenuTreeList);
        return success;
    }

    @GetMapping("/{menuId}")
    public RestResult getInfo(@PathVariable Long menuId) {
        SysMenu sysMenu = iSysMenuService.selectMenuById(menuId);
        if (StringUtils.isNull(sysMenu))
            return RestResult.error("查询失败,请检查menuId");
        return RestResult.success(sysMenu);
    }

    @PostMapping
    public RestResult add(@RequestBody @Validated SysMenuVO sysMenuVO) {
        if (UserConstants.NOT_UNIQUE.equals(iSysMenuService.checkMenuNameUnique(sysMenuVO)))
            return RestResult.error("新增菜单:" + sysMenuVO.getMenuName() + "失败,名称已存在");
        sysMenuVO.setCreateBy("system");
        return toAjax(iSysMenuService.insertMenu(sysMenuVO) ? 1 : 0);
    }

    @PutMapping
    public RestResult edit(@RequestBody @Validated SysMenuVO sysMenuVO) {
        if (UserConstants.NOT_UNIQUE.equals(iSysMenuService.checkMenuNameUnique(sysMenuVO)))
            return RestResult.error("修改菜单:" + sysMenuVO.getMenuName() + "失败,名称已存在");
        //TODO 从token中获取
        sysMenuVO.setUpdateBy("system");
        return toAjax(iSysMenuService.updateMenu(sysMenuVO) ? 1 : 0);
    }

    @DeleteMapping("/{menuId}")
    public RestResult deleteMenu(@PathVariable Long menuId) {
        if (iSysMenuService.hasChildByMenuId(menuId))
            return RestResult.error("存在子菜单,不允许删除");
        if (iSysMenuService.checkMenuExistRole(menuId))
            return RestResult.error("菜单已分配,不允许删除");
        return toAjax(iSysMenuService.deleteMenuById(menuId) ? 1 : 0);
    }
}
